﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml.Serialization;
using Report.Entity;
using Report.Properties;
using System.Text;

namespace Report.Service
{
    public class GomezService
    {
        private readonly HttpService httpService=new HttpService();

        public bool Login()
        {
            Dictionary<string, string> parameter = new Dictionary<string, string>
                                                       {
                                                           {"action", "login"},
                                                           {"form_string", ""},
                                                           {"pwd", Settings.Default.PassWord},
                                                           {"query_string", ""},
                                                           {"server_string", ""},
                                                           {"url_string", ""},
                                                           {"username", Settings.Default.User}
                                                       };

            HttpWebRequest webRequest = httpService.ConfigWebRequest(Resources.LoginUrl, Resources.LoginUrl, parameter);

            string responsetext = httpService.ExecuteRequestAsString(webRequest);

            //Set session value
            int start=responsetext.IndexOf("var paramList = \"")+17;
            int end = responsetext.IndexOf("\"",start);
            string sessionValues = responsetext.Substring(start, end-start);

            webRequest = httpService.ConfigWebRequest(Resources.SetSessionUrl, Resources.LoginUrl, Encoding.UTF8.GetBytes(sessionValues));
            responsetext = httpService.ExecuteRequestAsString(webRequest);

            return true;
        }

        public GomezTestCollection LoadTest()
        {
            HttpWebRequest webRequest = httpService.ConfigWebRequest(Resources.LoadTestUrl + "?AccountID=10763&Active=inactive&agentType=-1&bmSub=1");

            string responsetext = httpService.ExecuteRequestAsString(webRequest);

            XmlSerializer xmlSerializer=new XmlSerializer(typeof(GomezTestCollection));

            GomezTestCollection gomezTestCollection = (GomezTestCollection)xmlSerializer.Deserialize(new StringReader(responsetext));

            return gomezTestCollection;
        }

        public bool CreateChart(DateTime startDate, DateTime endDate)
        {
            Dictionary<string, string> parameter = new Dictionary<string, string>
                                                       {
                                                           {"datagroups", ""},
                                                           {"days", "all"},
                                                           {"endDate", "2011/07/15"},
                                                           {"endFilterDay", "1"},
                                                           {"endTime", endDate.ToString("hh:mm")},
                                                           {"period", "userdef"},
                                                           {"selAgentType", "-1"},
                                                           {"selectedMBGMids", "MBGMonitor:573248:true: 20091019_XD_10k.jpg_LM * (Pvt)"}, //?
                                                           {"showAllTests", "true"},
                                                           {"startDate", "2011/07/15"},
                                                           {"startFilterDay", "1"},
                                                           {"startFilterTime", "00:00"},
                                                           {"startTime", startDate.ToString("hh:mm")},
                                                           {"timeframe", "1"},
                                                           {"x", "51"}, //?
                                                           {"y", "2"}  //?
                                                       };

            HttpWebRequest webRequest = httpService.ConfigWebRequest(Resources.CreateChartUrl, Resources.CreateChartUrl, parameter);

            string responsetext = httpService.ExecuteRequestAsString(webRequest);

            return true;
        }
    }
}
